var token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJlNjkzMmI1Ny1iMWNmLTQyMmUtYjljMi03YjdmZDI4ZjM0ZGEiLCJzdWIiOiJBY2Nlc3MgVG9rZW4iLCJleHAiOjE3MDIwNDQyMTAsImlzcyI6IktvYWxhIERyaXZlciIsImlhdCI6MTcwMjA0MDYxMCwidXNlcklkIjoiMSIsInJvbGUiOiJ1c2VyIn0.rEi2xyRMy74E9fCqifM0CgnHTAwGHXaUeos8JTFvIto'

async function fetchData(url, method = 'GET', data) {
    if (localStorage.getItem('token') !== null) {
      token = localStorage.getItem('token')
    }
    let from = null;
    if (data) {
      from = new FormData();
      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {
          const element = data[key];
          from.append(key, element)
        }
      }
    }
    let requestOption = null;
    if (data) {
      requestOption = {
          method: method,
          headers: {
            'Authorization': `Bearer ${token}`,
            'Referrer-Policy': 'no-referrer',
          },
          body: from
      }
    } else {
      requestOption = {
        method: method,
        headers: {
          'Authorization': `Bearer ${token}`,
          'Referrer-Policy': 'no-referrer',
        }
      }
    }

    let resp = await fetch(url, requestOption);
    
    return await resp.json();
}

export default fetchData;